package com.alks.function.controller.biocontrol.deptwork;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.RoleCheck;
import com.alks.common.aop.WebLogger;
import com.alks.common.utils.DateUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.entity.PcProcessIePlan;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.request.biocontrol.deptwork.ProdPlanRequest;
import com.alks.function.data.request.biocontrol.deptwork.ProdPlanUpRequest;
import com.alks.function.service.biocontrol.deptwork.IProdPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Map;

import static com.alks.entity.data.enums.ResultCodeEnum.DEFAULT;

@RestController
@ApiRestController("/bioDeptWrok")
@Api(value = "生控作业", tags = "生控部-生控作业-产后计划安排")
@RequiredArgsConstructor
@RoleCheck(prgIds = "140105")
public class ProdPlanController {
    private final IProdPlanService service;

    /*资料区*/
    @PutMapping("/getProdPlanQuery")
    @ApiOperation(value = "产后计划安排-查询", tags = "生控部-生控作业-产后计划安排")
    public ResponseInfo getProdPlanQuery(@RequestBody ProdPlanRequest request) {
        return service.getProdPlanQuery(request);
    }

    @PutMapping("/prodPlanUpdate")
    @ApiOperation(value = "产后计划安排修改-更新", tags = "生控部-生控作业-产后计划安排")
    public ResponseInfo prodPlanUpdate(@RequestBody ProdPlanUpRequest request) {
        return service.prodPlanUpdate(request);
    }

    /*操作区*/
    @GetMapping("/getDeptPersonQuery")
    @ApiOperation(value = "操作区-查询", tags = "生控部-生控作业-产后计划安排")
    public ResponseInfo getDeptPersonQuery() {
        return service.getDeptPersonQuery();
    }

    @GetMapping("/deptPersonUpdate")
    @ApiOperation(value = "操作区修改-更新", tags = "生控部-生控作业-产后计划安排")
    public ResponseInfo deptPersonUpdate(@RequestParam @ApiParam("成型人数") BigDecimal lastPersons,
                                         @RequestParam @ApiParam("针车人数") BigDecimal stitchPersons,
                                         @RequestParam @ApiParam("裁断人数") BigDecimal cutPersons) {
        return service.deptPersonUpdate(lastPersons, stitchPersons, cutPersons);
    }

    @GetMapping("/prodLotUpdate")
    @ApiOperation(value = "更新指令-更新", tags = "生控部-生控作业-产后计划安排")
    public ResponseInfo prodLotUpdate() {
        return service.prodLotUpdate();
    }

    @GetMapping("/prodDataUpdate")
    @ApiOperation(value = "更新资料-更新", tags = "生控部-生控作业-产后计划安排")
    public ResponseInfo prodDataUpdate() {
        return service.prodDataUpdate();
    }

    /*工序产能设定*/
    @GetMapping("/getIePlanQuery")
    @ApiOperation(value = "工序产能设定-查询", tags = "生控部-生控作业-产后计划安排")
    public ResponseInfo getIePlanQuery() {
        return service.getIePlanQuery();
    }

    @PutMapping("/IePlanUpdate")
    @ApiOperation(value = "工序产能修改-更新", tags = "生控部-生控作业-产后计划安排")
    public ResponseInfo iePlanUpdate(@RequestBody PcProcessIePlan request,
                                     @RequestParam @ApiParam("旧序号") String seqNoOld) {
        return service.IePlanUpdate(request, seqNoOld);
    }

    @PutMapping("/iePlanAdd")
    @ApiOperation(value = "工序产能新增-新增", tags = "生控部-生控作业-产后计划安排")
    public ResponseInfo iePlanAdd(@RequestBody PcProcessIePlan request) {
        return service.iePlanAdd(request);
    }

    @PostMapping("/iePlanDelete")
    @ApiOperation(value = "工序产能删除-删除", tags = "生控部-生控作业-产后计划安排")
    public ResponseInfo iePlanDelete(@RequestParam @ApiParam("序号") String seqNo) {
        return service.iePlanDelete(seqNo);
    }

    @WebLogger
    @PutMapping("/prodLotExpord")
    @ApiOperation(value = "导出-导出EXCEL", tags = "生控部-生控作业-产后计划安排")
    public ResponseInfo prodLotExpord(@RequestBody ProdPlanRequest request, HttpServletResponse response){
        request.setPageSize(-1);
        Map<String, Object> data = service.getProdPlanQuery(request).getData();
        try {
            ExcelUtils.creatByTemplate(response, "产后计划模版","产后_"+ DateUtils.dateTimeNow(), data.get("dtos"));
        }catch (IOException e){
            return ResponseInfo.error("文件导出功能异常", DEFAULT.getCode());
        }
        return null;
    }


}
